package models

import (
	"exporter/dao"
	"strings"
	"time"
)

// Process Model
type Process struct {
	ID         int    `json:"id"`
	Name       string `json:"name"`
	Command    string `json:"command"`
	LogPath    string `json:"logPath"`
	SysLogFile string `json:"sysLogFile"`
	IP         string `json:"ip"`
	//Count int `json:"count"`
	Pid      int    `json:"pid"`
	Comments string `json:"comments"`
	//KeyWord string `json:"keyWord"`
	ShutdownCmd string    `json:"shutdownCmd"`
	Status      bool      `json:"status"`
	StartTime   time.Time `json:"startTime"`
	UpdateTime  time.Time `json:"updateTime"`
}

var metricStr string = `
# TYPE ${gauge} gauge
# HELP ${gauge} for process ${Name} active monitor.
emg_loader_cdr{label="${ProcName}",node="$IP"} ${value}
          `

/*
	Todo这个Model的增删改查操作都放在这里
*/
// CreateATodo 创建todo
func CreateAProcess(prc *Process) (err error) {
	err = dao.DB.Create(&prc).Error
	return
}

func GetAllProcess() (prcList []*Process, err error) {
	if err = dao.DB.Find(&prcList).Error; err != nil {
		return nil, err
	}
	return
}

func GetProcessList(ids string) (prcList []*Process, err error) {
	idList := strings.Split(ids, ",")
	if err = dao.DB.Where("id IN (?)", idList).Find(&prcList).Error; err != nil {
		return nil, err
	}
	return
}

func GetAProcess(id string) (prc *Process, err error) {
	prc = new(Process)
	if err = dao.DB.Debug().Where("id=?", id).First(prc).Error; err != nil {
		return nil, err
	}
	return
}

func UpdateAProcess(prc *Process) (err error) {
	err = dao.DB.Save(prc).Error
	return
}

func DeleteAProcess(id string) (err error) {
	err = dao.DB.Where("id=?", id).Delete(&Process{}).Error
	return
}
